﻿#include "ListWidgetPropertyEditEntry.h"
#include <QListWidget>

#include "IntegerEditEntry.h"
#include "BooleanEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(ListWidget)

struct ListWidgetPropertyEditEntry::ListWidgetPropertyEditEntryPrivate {
    QPointer<QListWidget> listWidget;

    QPointer<IntegerEditEntry> entryCurrentRow;
    QPointer<BooleanEditEntry> entrySortingEnabled;
};

ListWidgetPropertyEditEntry::~ListWidgetPropertyEditEntry() { }

ListWidgetPropertyEditEntry::ListWidgetPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new ListWidgetPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

ListWidgetPropertyEditEntry::ListWidgetPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new ListWidgetPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void ListWidgetPropertyEditEntry::setObject(void *object) {
    p->listWidget = static_cast<QListWidget*>(object);
}

void ListWidgetPropertyEditEntry::onTick() {
    if(!p->listWidget || !isVisible()) {
        return ;
    }

    p->entryCurrentRow->setValue(p->listWidget->currentRow());
    p->entrySortingEnabled->setValue(p->listWidget->isSortingEnabled());
}

void ListWidgetPropertyEditEntry::initUi() {
    p->entryCurrentRow = new IntegerEditEntry;
    appendChildEntry(p->entryCurrentRow);

    p->entrySortingEnabled = new BooleanEditEntry;
    appendChildEntry(p->entrySortingEnabled);
}

void ListWidgetPropertyEditEntry::retranslateUi() {
    setTitle(tr("QListWidget"));
    p->entryCurrentRow->setTitle(tr("currentRow"));
    p->entrySortingEnabled->setTitle(tr("sortingEnabled"));
}
